// pages/changepwd/changepwd.js
const util = require('../../utils/util.js');
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },

    /**
     * 表单提交
     */
    formSubmit: function (event) {
        const formData = event.detail.value
        if (formData.oldpwd === "" || formData.newpwd === "") {
            util.openErrorToast('密码不能为空')
            return
        }
        if (app.globalData.userInfo) {
            let url = app.globalData.httpBase + "/setNewPwd"
            let data = {
                openid: app.globalData.openid,
                oldpwd: formData.oldpwd,
                newpwd: formData.newpwd
            }
            //修改密码
            util.newhttp(url, 'POST', data).then(function (res) {
                let data = res.data
                switch (data.ret) {
                    case 0:
                        util.openErrorToast(data.message)
                        break
                    case 1:
                        util.openToast(data.message)
                        setTimeout(function () {
                            wx.switchTab({
                                url: '../center/center',
                            })
                        }, 1000)
                        break
                }

            }).catch(function (reason) {
                util.openErrorToast('网络错误')
            })

        } else {
            util.openErrorToast('请先登录')
            setTimeout(function () {
                wx.redirectTo({
                    url: '../login/login'
                })
            }, 1000)

        }

    },


})